<?php
defined('IN_SITE') or exit('Access Denied');

/*require_once 'tree.class.php';
require_once MOD_ROOT.'category/category.class.php';
require_once MOD_ROOT.'content/content.class.php';

$tree = new tree;
$catid = isset($catid) ? intval($catid) : 0;
$cat = new category();
$c = new content();*/

/* debug by aming */
if($_GET['debug']){
	$debugs = $db->get_one("select * from ".DB_PRE."category where catid = $catid");
	dump($debugs);
}


switch($action)
{
	default:
		
		echo $catdir;
		// 栏目首页
		if(!$catid && !$catdir){
			include template('category.html');exit();
		}
		
		// 如果参数是栏目名称
		if(!$catid && $catdir){
			$result = $db->get_one("select `catid`,`parentid` from ".DB_PRE."category where catdir = '$catdir' and parentdir = '$parentdir'");
			extract($result);
			unset($result);
		}
		
		if(/*$catid != 143*/1){
			/*如果该栏目下有子栏目 ，默认跳转到第一个子栏目下*/
			$twocat = $db->get_one("select * from ".DB_PRE."category where parentid = $catid");
			if($twocat){
				$catid = $twocat['catid'];
				
				// 二级栏目仍然存在子栏目
				$threecat = $db->get_one("select * from ".DB_PRE."category where parentid = $catid");
				if($threecat){
					$catid = $threecat['catid'];
					
					// 三级栏目仍然存在子栏目
					$fourcat = $db->get_one("select * from ".DB_PRE."category where parentid = $catid");
					if($fourcat){
						$catid = $fourcat['catid'];
						
						// 四级栏目仍然存在子栏目
						$fivecat = $db->get_one("select * from ".DB_PRE."category where parentid = $catid");
						if($fivecat){
							$catid = $fivecat['catid'];
						}
					}
					
				}
			}
		}


		// 这里对一直以来解决分页的问题有效
		$page = !$page || $page == 0 ?  1 : $page;
		
		$C = cache_read('category_'.$catid.'.php');
		extract($C);
		
		// 档栏目模型为内部栏目的时候
		if($type == 0)
		{
			//if($child || $arrchildid)
			if(subcat($catid))
			{
				$arrchildid = subcat($catid);
				$template = $template_category;
			}
			else
			{
				$template = $template_list;
			}
		}
		elseif($type == 1){
			
		}
		elseif($type == 2)
		{
			header('location:'.$url);
		}
		
		$catlist = submodelcat($modelid);
		$arrparentid = explode(',', $arrparentid);
		$parentid = $arrparentid[1];
		
		$head['title'] = $catname.'_'.$SETTING['sitename'];
		$head['keywords'] = $meta_keywords;
		$head['description'] = $meta_description;
		
		$ttl = $child == 1 ? CACHE_PAGE_CATEGORY_TTL : CACHE_PAGE_LIST_TTL;
		header('Last-Modified: '.gmdate('D, d M Y H:i:s', TIME).' GMT');
		header('Expires: '.gmdate('D, d M Y H:i:s', TIME + $ttl).' GMT');
		header('Cache-Control: max-age='.$ttl.', must-revalidate');
		
		include template($template);
		cache_page($ttl);exit();
	break;
	
	case 'load':
		
		if($modelid)
		{
			$CATEGORY = submodelcat($modelid);
		}
		$str = '<select onchange="$(\'#catid\').val(this.value);this.disabled=true;category_load(this.value);"><option value="0">'.$LANG['please_select'].'</option>';
		$options = '';
		foreach($CATEGORY as $i=>$v)
		{
			if((isset($id) && $v['parentid'] == $id) || (isset($module) && $v['module'] == $module))  $options .= '<option value="'.$i.'">'.$v['catname'].'</option>';
		}
		if(empty($options)) exit;
		$str .= $options.'</select>';
		
		echo $str;
	break;
	
	/*default:
		include template('category.html');
	break;*/
}
?>